﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAOMall;

namespace DTOMall
{
    public class ProductsDTO : DBTool, ICommon
    {
        public bool insert(object obj)
        {
            if (obj == typeof(Products))
            {
                try
                {
                    using (SqlConnection con = GetConnection())
                    {
                        Products p = (Products)obj;
                        using (SqlCommand cmd = con.CreateCommand())
                        {
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.CommandText = "Insert_Products";
                            cmd.Parameters.AddWithValue("@name", p.name);
                            cmd.Parameters.AddWithValue("@namePlain", p.name_plain);
                            cmd.Parameters.AddWithValue("@price", p.price);
                            cmd.Parameters.AddWithValue("@quantity", p.quantity);
                            cmd.Parameters.AddWithValue("@shopID", p.shop_id);
                            cmd.Parameters.AddWithValue("@description", p.description);
                            cmd.Parameters.AddWithValue("@content", p.content);
                            cmd.Parameters.AddWithValue("@createdDate", p.created_date);
                            cmd.Parameters.AddWithValue("@status", p.status);
                            object o = cmd.ExecuteScalar();
                            CloseConnection();
                            if (o == typeof(int))
                            {
                                return ((int)o > 0) ? true : false;
                            }
                            else
                            {
                                return false;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }

            }
            else
            {
                return false;
            }
        }

        public bool update(object obj)
        {
            if (obj == typeof(Products))
            {
                try
                {
                    using (SqlConnection con = GetConnection())
                    {
                        Products p = (Products)obj;
                        using (SqlCommand cmd = con.CreateCommand())
                        {
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.CommandText = "Update_Products";
                            cmd.Parameters.AddWithValue("@id", p.id);
                            cmd.Parameters.AddWithValue("@name", p.name);
                            cmd.Parameters.AddWithValue("@namePlain", p.name_plain);
                            cmd.Parameters.AddWithValue("@price", p.price);
                            cmd.Parameters.AddWithValue("@quantity", p.quantity);
                            cmd.Parameters.AddWithValue("@shopID", p.shop_id);
                            cmd.Parameters.AddWithValue("@description", p.description);
                            cmd.Parameters.AddWithValue("@content", p.content);
                            cmd.Parameters.AddWithValue("@status", p.status);
                            object o = cmd.ExecuteScalar();
                            CloseConnection();
                            if (o == typeof(int))
                            {
                                return ((int)o > 0) ? true : false;
                            }
                            else
                            {
                                return false;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            else
            {
                return false;
            }
        }

        public bool delete(int id)
        {
            try
            {
                using (SqlConnection con = GetConnection())
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "Delete_Products";
                        cmd.Parameters.AddWithValue("@id", id);
                        object o = cmd.ExecuteScalar();
                        if (o == typeof(int))
                        {
                            return ((int)o > 0) ? true : false;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public System.Data.DataTable getAll()
        {
            try
            {
                using (SqlConnection con = GetConnection())
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {

                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "View_All_Products";
                        DataTable dt = GetTable(cmd);
                        return dt;
                    }

                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public object getByID(int id)
        {
            try
            {
                using (SqlConnection con = GetConnection())
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "View_Products_By_ID";
                        cmd.Parameters.AddWithValue("@id", id);
                        DataTable dt = GetTable(cmd);
                        Products p = new Products();
                        p.id = id;
                        p.name = dt.Rows[0]["name"].ToString();
                        p.name_plain = dt.Rows[0]["name_plain"].ToString();
                        p.price = Convert.ToSingle(dt.Rows[0]["price"].ToString());
                        p.quantity = Convert.ToInt32(dt.Rows[0]["quantity"].ToString());
                        p.shop_id = Convert.ToInt32(dt.Rows[0]["shop_id"].ToString());
                        p.description = dt.Rows[0]["description"].ToString();
                        p.content = dt.Rows[0]["content"].ToString();
                        p.created_date = DateTimeLib.Convert2Date(dt.Rows[0]["created_date"].ToString());
                        p.status = Convert.ToInt32(dt.Rows[0]["status"].ToString());
                        return p;
                    }

                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #region SearchByName
        public DataTable Search(string name) {
            try
            {
                using (SqlConnection con = GetConnection())
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "Search_Product";
                        cmd.Parameters.AddWithValue("@name", name);
                        DataTable dt = GetTable(cmd);
                        return dt;
                    }

                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion
    }
}
